//
// XMLSAXReaderEngine
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.core;

import java.io.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XMLSAXReaderEngine extends DefaultHandler
{
    // ============================================================
    // Fields
    // ============================================================

    private XMLSAXReader parser;
    private Stack<String> stateStack;

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void parse( XMLSAXReader parser, InputStream in )
        throws IOException, SAXException
    {
        this.parser = parser;
        stateStack = new Stack<String>();

        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setErrorHandler( new ReaderErrorHandler() );
        reader.setContentHandler( this );
        reader.parse( new InputSource( in ) );
    }

    @Override
    public void startDocument()
        throws SAXException
    {
        try
        {
            parser.startDocument();
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }

    @Override
    public void endDocument()
        throws SAXException
    {
        try
        {
            parser.endDocument();
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }

    @Override
    public void startElement( String uri, String tagName, String qName, Attributes atts )
        throws SAXException
    {
        try
        {
            String newState = parser.startElement( getState(), uri, tagName, qName, atts );
            pushState( newState );
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }

    @Override
    public void characters( char[] value, int offset, int count )
        throws SAXException
    {
        try
        {
            parser.characters( getState(), value, offset, count );
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }

    @Override
    public void endElement( String uri, String tagName, String qName )
        throws SAXException
    {
        try
        {
            parser.endElement( getState(), uri, tagName, qName );

            popState();
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }

    // ----------
    // protected
    // ----------

    protected String getState()
    {
        if ( stateStack.isEmpty() )
        {
            return null;
        }

        return stateStack.peek();
    }

    protected String popState()
    {
        String state = null;

        stateStack.pop();
        if ( !stateStack.isEmpty() )
        {
            state = stateStack.peek();
        }

        return state;
    }

    protected void pushState( String state )
    {
        stateStack.push( state );
    }

    // ============================================================
    // Inner Classes
    // ============================================================

    public class ReaderErrorHandler implements ErrorHandler
    {
        @Override
        public void warning( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }

        @Override
        public void error( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }

        @Override
        public void fatalError( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }
    }
}

